package com.cwj.mansysback.dao.impl;

import com.cwj.mansysback.dao.BaseDao;
import com.cwj.mansysback.dao.api.ElectiveDao;
import com.cwj.mansysback.entity.Elective;
import com.cwj.mansysback.entity.Pack;
import com.cwj.mansysback.service.api.ElectiveService;

import java.util.List;

public class ElectiveDaoImpl extends BaseDao<Elective> implements ElectiveDao {

    private static final String TABLE_NAME="electives";

    @Override
    public boolean insertElective(String cid, String sid, double cscore) {

        String sql = "insert into "+ElectiveDaoImpl.TABLE_NAME+"\t(cid,sid,cscore) values (?,?,?)";

        return super.update(sql,cid,sid,cscore) > 0;
    }

    @Override
    public boolean deleteElective(String cid, String sid) {

        String sql = "delete from "+ElectiveDaoImpl.TABLE_NAME + "\t where cid = ? and sid = ?";

        return super.update(sql,cid,sid) > 0;
    }

    @Override
    public Elective selectElective(String cid, String sid) {
        String sql = "select * from "+ElectiveDaoImpl.TABLE_NAME+"\twhere cid = ? and sid = ?";
        return super.select(Elective.class,sql,cid,sid);
    }

    @Override
    public List<Elective> selectCidElective(String cid, int start, int len) {

        String sql = "select * from "+ElectiveDaoImpl.TABLE_NAME+"\t where cid = ? limit ?,?";
        System.out.println(sql);

        return super.selectBatch(Elective.class,sql,cid,(start-1)*Math.max(len,0),len);
    }

    @Override
    public List<Elective> selectSidElective(String sid, int start, int len) {

        String sql = "select * from "+ElectiveDaoImpl.TABLE_NAME+"\twhere sid = ? limit ?,?";



        return super.selectBatch(Elective.class,sql,sid,Math.max((start-1)*len,0),len);
    }

    @Override
    public List<Elective> selectElective(int start, int len) {

        String sql = "select * from "+ElectiveDaoImpl.TABLE_NAME+"\twhere limit ?,?";

        return super.selectBatch(Elective.class,sql,start,len);
    }

    @Override
    public Pack selectPackBySid(String sid) {

        String sql = "select count(*) as `count` from "+ElectiveDaoImpl.TABLE_NAME+"\twhere sid = ?";

        return new BaseDao<Pack>().select(Pack.class,sql,sid);
    }
    @Override
    public Pack selectPackByCid(String cid) {

        String sql = "select count(*) as `count` from "+ElectiveDaoImpl.TABLE_NAME+"\twhere cid = ?";

        return new BaseDao<Pack>().select(Pack.class,sql,cid);
    }
    @Override
    public Pack selectPack() {

        String sql = "select count(*) as `count` from "+ElectiveDaoImpl.TABLE_NAME;

        return new BaseDao<Pack>().select(Pack.class,sql);
    }
    @Override
    public boolean updateEletive(String cid,String sid,double cscore){
        String sql = "update "+ ElectiveDaoImpl.TABLE_NAME +"\tset cscore = ? where cid = ? and sid = ?";
        return super.update(sql,cscore,cid,sid) > 0;
    }
}
